package com.hdax.dm.controller;

import com.hdax.dm.exception.DmException;
import com.hdax.dm.service.PayService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

@RestController
@RequestMapping("/pay")
public class PayController {

    @Autowired
    private PayService payService;

    @PostMapping("buy")
    public String pay(@RequestBody Map<String,String> map) throws Exception {
        String str;
        try {
            str= payService.pay(map.get("orderNo"));
        }catch (DmException e){
            System.out.println(e.getMessage());
            throw e;
        }
        return str;
    }

    @PostMapping("confirmpay")
    public CommonResponse<Map<String, String>> confirmpay(@RequestBody Map<String,String> map){
        return payService.confirmpay(map.get("orderNo"));
    }

    //异常处理
    @ExceptionHandler(value = {DmException.class})
    public CommonResponse<String> ExceptionHandler(DmException e){
        return ResponseUtil.returnFail(e.hashCode()+"",e.getMessage());
    }
}
